#!/bin/bash

if (($# < 1)); then
    exit 1
fi

sid=$1

title="backup s$sid"

db_name=`./config/game_db_name.sh $sid`
if [ -z $db_name ]; then
    ./util/error.sh "$title" "db name invalid"
    exit 1
fi

db_address=`./config/game_db_address.sh $sid`
if [ -z $db_address ]; then
    ./util/error.sh "$title" "db address invalid"
    exit 1
fi

./game stop $sid
if (($? != 0)); then
    ./util/error.sh "$title" "stop failed"
    exit 1
fi
exit 0
sleep 1


bak_db_name="${db_name}_bak"
backup_address=`./config/get.sh merge.mongo_address`
./util/debug.sh "$title" "copy ${db_address}/${db_name} to ${backup_address}/${bak_db_name} ..."
mongo $backup_address/$bak_db_name --quiet --eval "db.dropDatabase()"
mongo $backup_address --quiet --eval "db.copyDatabase('$db_name', '$bak_db_name', '$db_address')"
if (($? != 0)); then
    ./util/error.sh "$title" "copy db failed"
    exit 1
fi
sleep 1
